package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.Order;
import com.cskaoyan.bean.lzp.*;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.OrderGoodsMapper;

import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderGoodsMapper orderGoodsMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public OrderDetailVO queryDetail(String id) {

        Order order = orderMapper.selectByPrimaryKey(Integer.parseInt(id));

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        if (id != null && !"".equals(id)) {
            criteria.andOrderIdEqualTo(Integer.parseInt(id));
        }
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        User user = userMapper.selectByPrimaryKey(order.getUserId());

        return new OrderDetailVO(order, orderGoods, user);
    }

    @Override
    public int deleteByOrderId(Integer orderId) {
        int i = orderMapper.deleteByPrimaryKey(orderId);
        return i;
    }

    @Override
    public List<OrderChannel> queryChannel() {
        ArrayList<OrderChannel> orderChannels = new ArrayList<>();
        orderChannels.add(new OrderChannel("ZTO", "中通快递"));
        orderChannels.add(new OrderChannel("YTO", "圆通速递"));
        orderChannels.add(new OrderChannel("YD", "韵达速递"));
        orderChannels.add(new OrderChannel("YZPY", "邮政快递包裹"));
        orderChannels.add(new OrderChannel("EMS", "EMS"));
        orderChannels.add(new OrderChannel("DBL", "德邦快递"));
        orderChannels.add(new OrderChannel("FAST", "快捷快递"));
        orderChannels.add(new OrderChannel("ZJS", "宅急送"));
        orderChannels.add(new OrderChannel("TNT", "TNT快递"));
        orderChannels.add(new OrderChannel("UPS", "UPS"));
        orderChannels.add(new OrderChannel("DHL", "DHL"));
        orderChannels.add(new OrderChannel("FEDEX", "FEDEX联邦(国内件)"));
        orderChannels.add(new OrderChannel("FEDEX_GJ", "FEDEX联邦(国际件)"));
        return orderChannels;
    }

    @Override

    public OrderListData list(BaseParam param, OrderListBO orderListBO) {

        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        PageHelper.startPage(page, limit);

        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause(sort + " " + order);

        if (orderListBO.getUseId() != null) {
            criteria.andUserIdEqualTo(orderListBO.getUseId());
        }
        if (orderListBO.getOrderSn() != null && "".equals(orderListBO.getOrderSn())) {
            criteria.andOrderSnEqualTo(orderListBO.getOrderSn());
        }
        if (orderListBO.getTimeArray() != null) {
            criteria.andAddTimeBetween(orderListBO.getStart(), orderListBO.getEnd());
        }

        Integer[] statusArray = orderListBO.getOrderStatusArray();

        if (statusArray != null && statusArray.length > 0) {
            ArrayList<Short> arrayList = new ArrayList<>();
            for (Integer integer : statusArray) {
                arrayList.add(integer.shortValue());
            }
            criteria.andOrderStatusIn(arrayList);
        }

        criteria.andDeletedEqualTo(false);
        List<Order> list = orderMapper.selectByExample(example);

        PageInfo<Order> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return OrderListData.data(total, pages, limit, page, list);
    }


    public GoodsStatBean statOrderGoods() {
        //查询数据库
        List<GoodsRows> rows = orderMapper.statOrderGoods();
        List<String> columns = new ArrayList<>();
        columns.add(0, "day");
        columns.add(1, "orders");
        columns.add(2, "products");
        columns.add(3, "amount");
        GoodsStatBean goodsStatBean = new GoodsStatBean();
        goodsStatBean.setColumns(columns);
        goodsStatBean.setRows(rows);
        return goodsStatBean;
    }

    @Override
    public OrderStatBean order() {
        //查询数据库
        List<OrderRows> rows = orderMapper.statOrder();
        List<String> columns = new ArrayList<>();
        columns.add(0, "day");
        columns.add(1, "orders");
        columns.add(2, "customers");
        columns.add(3, "amount");
        columns.add(4, "pcr");
        OrderStatBean orderStatBean = new OrderStatBean();
        orderStatBean.setColumns(columns);
        orderStatBean.setRows(rows);
        return orderStatBean;
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:28
     * @Param: [showType, page, limit, id]:
     * @return: com.cskaoyan.bean.WxOrderListBean:
     **/
    @Override
    public WxOrderListBean wxlist(Integer showType, Integer page, Integer limit, Integer id) {
        //
        //查询总共所有的数量
        String sort = "id";//排序列---订单id，新到旧
        String order = "desc";//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        Integer type = -1;
        if (showType == 0) {
            type = 0;
        } else if (showType == 1) {
            type = 101;
        } else if (showType == 2) {
            type = 201;
        } else if (showType == 3) {
            type = 301;
        } else if (showType == 4) {
            type = 401;//TODO:此处应该包含402状态码
        }
        List<WxOrderListVO> list = orderMapper.selectOrdersByUserId(sort, order, id, type);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<WxOrderListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return WxOrderListBean.data(total, pages, limit, page, list);

    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:28
     * @Param: [orderId]:
     * @return: com.cskaoyan.bean.WxOrderDetailBean:
     **/
    @Override
    public WxOrderDetailBean wxOrderDetail(Integer orderId) {
        //查詢数据库
        WxOrderDetailBean wxOrderDetailBean = orderMapper.queryWxOrderDetail(orderId);
        List<WxOrderGoods> wxOrderGoods = orderMapper.queryWxOrderGoodsDetail(orderId);
        wxOrderDetailBean.setOrderGoods(wxOrderGoods);
        return wxOrderDetailBean;
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:28
     * @Param: [orderId]:
     * @return: int:
     **/
    @Override
    public int wxCancelOrder(Integer orderId) {
        //改变订单状态
        int affectedRows = orderMapper.wxCancelOrder(orderId);
        if (affectedRows == 1) {
            return 200;
        }
        return 0;
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:28
     * @Param: [orderId]:
     * @return: int:
     **/
    @Override
    public int wxDeleteOrder(Integer orderId) {
        //逻辑删除
        int affectedRows = orderMapper.wxDeleteOrder(orderId);
        int affectedRows2 = orderMapper.wxDeleteOrderGppds(orderId);
        return 200;
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:28
     * @Param: [orderId]:
     * @return: int:
     **/
    @Override
    public int wxConfirmOrder(Integer orderId) {
        //在确认收货是，更新待评价商品的数量
        //通过订单id，查询订单中商品数量，设置待评价商品的值
        int count = orderMapper.queryOrderGoodsByOrderId(orderId);

        Order order = new Order(orderId, null, null, 401, null, null, null, null, null, null, null, null, null, null, null, null, null, null
                , null, null, null, null, null, null, null, new Date(System.currentTimeMillis()), count, null, null, new Date(System.currentTimeMillis()), null);
        int affectedRows = orderMapper.updateByPrimaryKeySelective(order);
        return 200;
    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:27
     * @Param: [orderId]:
     * @return: int:
     **/
    @Override
    public int wxRefundOrder(Integer orderId) {
        //申请退款---订单取消，状态码改为202
        Order order = new Order(orderId, null, null, 202, null, null, null, null, null, null, null, null, null, null, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null, null, new Date(System.currentTimeMillis()), null);
        int affectedRows = orderMapper.updateByPrimaryKeySelective(order);
        return 200;
    }

    /**
     * @Description: 付款
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 15:27
     * @Param: [orderId]:
     * @return: int:
     **/
    @Override
    public int wxPrepayOrder(Integer orderId) {
        //逻辑付款
        Order order = new Order(orderId, null, null, 201, null, null, null, null, null, null, null, null, null, null, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null, null, new Date(System.currentTimeMillis()), null);
        int affectedRows = orderMapper.updateByPrimaryKeySelective(order);
        return 200;

    }

    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 16:49
     * @Param: [orderId, goodsId]:
     * @return: com.cskaoyan.bean.WxOrderGoods:
     **/
    @Override
    public WxOrderGoods wxGoods(Integer orderId, Integer goodsId) {
        WxOrderGoods wxOrderGoods = orderMapper.queryWxOrderGoodsByOrderIdAndGoodsId(orderId, goodsId);

        return wxOrderGoods;
    }


    @Autowired
    CommentMapper commentMapper;


    /**
     * @Description:
     * @Author: cuiweiguo#
     * @Date: 2022/5/9 16:49
     * @Param: [wxOrderParamBean, userId]:
     * @return: int:
     **/
    @Override
    public int comment(WxOrderParamBean wxOrderParamBean, Integer userId) {
        //先查询order_goods表获得商品id---》valueId
        Integer goodsId = orderMapper.selectGoodsIdByPrimaryKey(wxOrderParamBean.getOrderGoodsId());


        Comment comment = new Comment(null, goodsId, null, wxOrderParamBean.getContent(),
                null, userId, wxOrderParamBean.getHasPicture(), wxOrderParamBean.getPicUrls(),
                wxOrderParamBean.getStar(), new Date(System.currentTimeMillis()),
                new Date(System.currentTimeMillis()), null);
        //更新评价表
        commentMapper.insertSelective(comment);
        //获得评价表的自增id
        Integer commentId = comment.getId();
        //再order——goods表更新comment
        OrderGoods orderGoods = new OrderGoods(wxOrderParamBean.getOrderGoodsId(), null, null, null, null, null, null,
                null, null, null, commentId, null, new Date(System.currentTimeMillis()), null);
        orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);
        return 200;
    }

    @Override
    public Integer refund(OrderRefundBO orderRefundBO) {
        Order order = new Order();
        order.setId(orderRefundBO.getOrderId());
        order.setRefundAmount(BigDecimal.valueOf(orderRefundBO.getRefundMoney()));
        order.setRefundTime(new Date());
        order.setUpdateTime(new Date());
        order.setOrderStatus(203);
        int i = orderMapper.updateByPrimaryKeySelective(order);
        //更新成功 返回1
        if (i > 0) {
            return 1;
        }
        //更新失败 返回0
        return 0;
    }

    @Override
    public Integer ship(OrderShipBO orderShipBO) {
        Order order = new Order();
        order.setId(orderShipBO.getOrderId());
        order.setShipChannel(orderShipBO.getShipChannel());
        order.setShipSn(orderShipBO.getShipSn());
        order.setUpdateTime(new Date());
        order.setShipTime(new Date());
        order.setOrderStatus(301);
        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i>0){
            return 1;
        }
        return 0;
    }
}

